package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.CategoryConstant;
import com.powernode.domain.Category;
import com.powernode.ex.ProductException;
import com.powernode.mapper.CategoryMapper;
import com.powernode.service.CategoryService;
import com.powernode.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 内容:
 * 作者: cy
 * RedisTemplate   序列化的问题
 */
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类集合
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_ALL_PREFIX)
    public List<Category> list() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getSeq)
        );
    }

    /**
     * 查询父分类集合
     * 分类 菜单  层级要限制一下  不要无限的多
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_PARENT_PREFIX)
    public List<Category> selectCategoryParentList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0L)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq)
        );

    }

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @Override
    @Caching(
            evict = {
                    @CacheEvict(key = CategoryConstant.CATEGORY_PARENT_PREFIX),
                    @CacheEvict(key = CategoryConstant.CATEGORY_ALL_PREFIX)
            }
    )
    public boolean save(Category category) {
        Long userId = AuthUtil.getUserIdOrShopId(true);
        log.info("新增分类{},当前用户id为{}", category, userId);
        category.setRecTime(new Date());
        category.setUpdateTime(new Date());
        Long parentId = category.getParentId();
        if (parentId != null && parentId != 0L) {
            // 选了一个父亲
            // 校验一下
            Category root = categoryMapper.selectById(parentId);
            if (ObjectUtils.isEmpty(root)) {
                log.error("新增分类是父分类不存在，父分类id为{}", parentId);
                throw new ProductException("新增分类是父分类不存在");
            }
        } else {
            category.setParentId(0L);
        }
        return categoryMapper.insert(category) > 0;
    }


    /**
     * 查询父分类集合
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Category> selectFrontCategoryByPid(Long parentId) {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq)
        );
    }
}
